﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MobileServer.Bll.Bis;

namespace Bingosoft.Swimming.WebServer
{
    public partial class Activity : System.Web.UI.Page
    {
        UserInfo currentUser;
        UserService userService = new UserService();
        ActivityService activitySerivce = new ActivityService();

        protected void Page_Load(object sender, EventArgs e)
        {
            currentUser = userService.GetUserInfo(Bingosoft.Security.SecurityContext.User.Id);
            UserName.Text = currentUser.Name;

            if (!IsPostBack)
                BindData();
        }

        private void BindData()
        {
            this.rptActivity.DataSource = activitySerivce.GetActivitysWithoutEnd();
            this.rptActivity.DataBind();
        }


        protected void rptActivity_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string activityId = e.CommandArgument.ToString();
            switch (e.CommandName)
            {
                case "Enter":
                    userService.SetEnter(activityId, currentUser.Id.ToString());
                    break;
                case "NotEnter":
                    userService.SetNotEnter(activityId, currentUser.Id.ToString());
                    break;
            }
            Response.Redirect(Request.RawUrl);
        }

        protected void rptActivity_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                ActivityInfo activity = (ActivityInfo)e.Item.DataItem;
                DataList EnterUsers = (DataList)e.Item.FindControl("EnterUsers");
                ImageButton NotEnter = (ImageButton)e.Item.FindControl("NotEnter");
                ImageButton Enter = (ImageButton)e.Item.FindControl("Enter");

                var activityUsers = activitySerivce.GetActivityUsers(activity.Id.ToString());

                EnterUsers.DataSource = activityUsers;
                EnterUsers.DataBind();

                Enter.Visible = !(NotEnter.Visible = activityUsers.Exists(o => o.UserID == currentUser.Id && o.IsEnter));
            }
        }
    }
}